//
// Created by kriko on 2021/7/2.
//

#ifndef CSAPP_ECHOSERVER_PRE_H
#define CSAPP_ECHOSERVER_PRE_H

#include <csapp.h>

static int byte_cnt;
static sem_t mutex;

static void init_echo_cnt(void) {
    sem_init(&mutex, 0, 1);
    byte_cnt = 0;
}

void echo_cnt(int connfd) {
    int n;
    char buf[MAXLINE];
    rio_t  rio;
    static pthread_once_t once = PTHREAD_ONCE_INIT;

    pthread_once(&once, init_echo_cnt);

    Rio_readinitb(&rio, connfd);
    while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) {
        P(&mutex);
        byte_cnt += n;
        printf("server received %d (%d total) bytes on fd %d \n", n, byte_cnt, connfd);
        V(&mutex);
        Rio_writen(connfd, buf, n);
    }
}

#endif //CSAPP_ECHOSERVER_PRE_H
